package com.ruoyi.common.core.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author zrd
 * @version 1.0
 * @date 2020/12/16 17:12
 */
@Configuration
//@MapperScan(basePackages = {"com.xinjiang.**.mapper"})
@EnableTransactionManagement
@ConditionalOnClass(value = {PaginationInnerInterceptor.class})
public class MybatisPlusConfig {


    /**
     * ø
     * 性能分析拦截器，不建议生产使用
     */
//    @Bean
//    public PerformanceInterceptor performanceInterceptor(){
//        return new PerformanceInterceptor();
//    }

    //
    //@Bean
    //public PaginationInterceptor paginationInterceptor() {
    //    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    //    // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
    //    // paginationInterceptor.setOverflow(false);
    //    // 设置最大单页限制数量，默认 500 条，-1 不受限制
    //    // paginationInterceptor.setLimit(500);
    //    // 开启 count 的 join 优化,只针对部分 left join
    //    paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
    //    return paginationInterceptor;
    //}

    ///**
    // * 分页插件 ps: 如果引入了pageHelper 就需要手动注入mybatisPlus的分页拦截器
    // */
    @Bean
    public MybatisPlusInterceptor innerInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //设置分页插件，一级和二级缓存和mybatis一样
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        //设置数据库类型
        paginationInterceptor.setDbType(DbType.MYSQL);
        //设置最大每页条数，默认为500条，-1不受限制
        paginationInterceptor.setMaxLimit(-1L);
        interceptor.addInnerInterceptor(paginationInterceptor);//这里是重点
        //设置乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //设置防全表更新与删除插件
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }
    ///**
    // * 分页插件
    // */
    //@Bean
    //public MybatisPlusInterceptor mybatisPlusInterceptor() {
    //    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    //    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    //    return interceptor;
    //}
//    @Bean
//    public MyMetaObjectHandler myMetaObjectHandler() {
//        return new MyMetaObjectHandler();
//    }

}
